package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.mapper.customerMapper;
import com.gxa.mapper.GoodsMapper;
import com.gxa.mapper.OrderFormMapper;
import com.gxa.mapper.orderMapper;
import com.gxa.param.MyTableParam;
import com.gxa.pojo.*;
import com.gxa.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
@Service
public class GoodsServicelmpl implements GoodsService {
    @Autowired
    private GoodsMapper mapper;

    @Autowired
    private orderMapper orderMapper;
    @Autowired
    private OrderFormMapper orderFormMapper;

    @Override
    public LayR list(MyTableParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());

        List<Goods> allForList = mapper.findAllForList(param);

        PageInfo<Goods> orderSelectPageInfo = new PageInfo<>(allForList);

        return new LayR(0,"success",orderSelectPageInfo.getList(),orderSelectPageInfo.getTotal());

    }

    @Override
    public R findById(Integer id) {
        Goods byPk = mapper.findByPk(id);

        return new R(200,"success",byPk);
    }

    @Override
    public R edit(Goods goods) {
        mapper.update(goods);

        return new R(200,"修改成功");
    }

    @Override
    public R buy(String[] ids,HttpSession session) {

        String goods_total_name="";

        double total_money=0.00;

        for (String id:ids){
            int i = Integer.parseInt(id);

            Goods byPk = mapper.findByPk(i);

            goods_total_name=goods_total_name +"  "+ byPk.getGoods();


            total_money=total_money+byPk.getNumber()*byPk.getPrices();
            
        }

        OrderSelect orderSelect = new OrderSelect();

        orderSelect.setGoods(goods_total_name);
        orderSelect.setCustomerId((Integer) session.getAttribute("customerId"));
        orderSelect.setOrderPrice(total_money);
        orderSelect.setOrderTime(new Timestamp(System.currentTimeMillis()));
        orderSelect.setStatus("等待收货");

        orderMapper.insert(orderSelect);


        orderFormMapper.update(total_money,(Integer) session.getAttribute("customerId"));


        return new R(200,"下单成功");
    }

}
